package com.dp.service.impl;

import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.service.impl3.BaseImpl;
import com.dp.util.DataMap;
import com.dp.vo.NewsClientcontrolone;

@Service
@Transactional
public class ClientcontroloneServiceImpl extends BaseImpl {
	
	/**
	 * Find all the NewsClientcontrolone Entity
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsClientcontrolone>
	 */
	public List<NewsClientcontrolone> findAllNewsClientcontrolone(DataMap hashMap, Integer firstrow,
			Integer maxrow){
		NewsClientcontrolone clientcontrol=(NewsClientcontrolone) hashMap.get("clientcontrolone");
		List<NewsClientcontrolone> queryList=null;
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsClientcontrolone n WHERE 1=1 ");
			if(clientcontrol!=null){
				if(clientcontrol.getClientid()!=null&& clientcontrol.getClientid().toString().trim().length() > 0){
					sf.append(" AND n.clientid='"
							+ clientcontrol.getClientid().toString().trim() + "'");
				}
			}
			sf.append(" order by clientid desc");
			Query query = em.createQuery(sf.toString());
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			queryList = query.getResultList();
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return queryList;
	}

	/**
	 * findNewsClientcontroloneByClientid
	 * @param clientid
	 * @return NewsClientcontrolone
	 */
	public NewsClientcontrolone findNewsClientcontroloneByClientid(Integer clientid) {
		NewsClientcontrolone o = null;
		try {
			o = this.em.find(NewsClientcontrolone.class, clientid);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		if (o == null) {
			String msg = "Uh oh, '" + NewsClientcontrolone.class + "' object with clientid '" + clientid
					+ "' not found...";
			logger.warn(msg);
			throw new EntityNotFoundException(msg);
		}
	
		return o;
	}

	
	/***
	 * 新增单个客户端控制
	 * addNewsClientcontrolone 
	 * @param DataMap
	 * @return int
	 * 1.终端id已被记录
	 * 2.终端id不存在，请确认
	 * 0.成功
	 * -1.失败
	 */
	public int addNewsClientcontrolone(DataMap hashMap) {
		NewsClientcontrolone clientcontrol=(NewsClientcontrolone) hashMap.get("clientcontrolone");
		int resultInt=0;
//		try {
//			this.init();
//			this.sql="SELECT clientid FROM news_clientcontrolone WHERE clientid=?";
//			this.prep=this.conn.prepareStatement(sql);	
//			this.prep.setInt(1, clientcontrol.getClientid());
//			this.rs1=prep.executeQuery();
//			if(this.rs1.next()){
//				//终端id已被记录
//				return 1;
//			}
//			this.sql="SELECT clientid FROM news_clientinfo WHERE clientid=?";
//			this.prep=this.conn.prepareStatement(sql);	
//			this.prep.setInt(1, clientcontrol.getClientid());
//			this.rs1=prep.executeQuery();
//			if(!this.rs1.next()){
//				//终端id不存在，请确认
//				return 2;
//			}
//			//增加单个客户端控制信息
//			this.sql="insert into news_clientcontrolone (clientid,smspf,live,uninstall,nexturl,smslive,smsuninstall,smsnexturl,smscontentspf,smssourcepf,maxsms,about,nexttime) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
//			this.prep=this.conn.prepareStatement(sql);
//			prep.setInt(1, clientcontrol.getClientid());	
//			prep.setString(2, clientcontrol.getSmspf());
//			prep.setString(3, clientcontrol.getLive());
//			prep.setString(4, clientcontrol.getUninstall());
//			prep.setString(5, clientcontrol.getNexturl());
//			prep.setString(6, clientcontrol.getSmslive());
//			prep.setString(7, clientcontrol.getSmsuninstall());
//			prep.setString(8, clientcontrol.getSmsnexturl());
//			prep.setString(9, clientcontrol.getSmscontentspf());
//			prep.setString(10, clientcontrol.getSmssourcepf());
//			prep.setString(11, clientcontrol.getMaxsms());
//			prep.setString(12, clientcontrol.getAbout());
//			prep.setString(13, clientcontrol.getNexttime());
//			prep.executeUpdate();
//			
//			conn.commit(); 
//		} catch (Exception e) {
//			logger.info("addNewsClientcontrolone fail !!!" + e.getMessage());
//			e.printStackTrace();
//			resultInt=-1;
//			if(conn!=null){
//				try {
//					conn.rollback();
//				} catch (SQLException e1) {
//					e1.printStackTrace();
//				} 
//			}
//		} finally {
//			this.closeAll();
//		}
		return resultInt;
	}
	
	
	
	/**
	 * 删除单个客户端控制
	 * @param clientid
	 * @return	Boolean
	 */
	public Boolean deleteNewsClientcontroloneByClientid(Integer clientid) {
		Boolean resultb=true;
		try {
			//删除单个客户端控制信息
			this.sql="delete from news_clientcontrolone where clientid="+clientid;
			this.em.createNativeQuery(sql).executeUpdate();
		} catch (Exception e) {
			logger.info("deleteNewsClientcontroloneByClientid fail !!!" + e.getMessage());
			e.printStackTrace();
			resultb=false;
		} 
		return resultb;
	}

	/***
	 * 更新单个客户端控制信息
	 * updateNewsClientcontrol 
	 * @param DataMap
	 * @return int
	 * 1.终端id已被记录
	 * 2.终端id不存在，请确认
	 * 0.成功
	 * -1.失败
	 */
	public int updateNewsClientcontrolone(DataMap hashMap) {
		NewsClientcontrolone clientcontrol=(NewsClientcontrolone) hashMap.get("clientcontrolone");
		String clientid_old=(String)hashMap.get("clientid_old");
		int resultInt=0;
//		try {
//			this.init();
//			this.sql="SELECT clientid FROM news_clientcontrolone WHERE clientid=? AND clientid!=?";
//			this.prep=this.conn.prepareStatement(sql);	
//			this.prep.setInt(1, clientcontrol.getClientid());
//			this.prep.setInt(2, Integer.parseInt(clientid_old));
//			this.rs1=prep.executeQuery();
//			if(this.rs1.next()){
//				//终端id已被记录
//				return 1;
//			}
//			this.sql="SELECT clientid FROM news_clientinfo WHERE clientid=?";
//			this.prep=this.conn.prepareStatement(sql);	
//			this.prep.setInt(1, clientcontrol.getClientid());
//			this.rs1=prep.executeQuery();
//			if(!this.rs1.next()){
//				//终端id不存在，请确认
//				return 2;
//			}
//			//更新单个客户端控制信息
//			this.sql="update news_clientcontrolone set clientid=?,smspf=?,live=?,uninstall=?,nexturl=?,smslive=?,smsuninstall=?,smsnexturl=?,smscontentspf=?,smssourcepf=?,maxsms=?,about=?,nexttime=? where clientid=?";
//			this.prep=this.conn.prepareStatement(sql);
//			prep.setInt(1, clientcontrol.getClientid());
//			prep.setString(2, clientcontrol.getSmspf());
//			prep.setString(3, clientcontrol.getLive());
//			prep.setString(4, clientcontrol.getUninstall());
//			prep.setString(5, clientcontrol.getNexturl());
//			prep.setString(6, clientcontrol.getSmslive());
//			prep.setString(7, clientcontrol.getSmsuninstall());
//			prep.setString(8, clientcontrol.getSmsnexturl());
//			prep.setString(9, clientcontrol.getSmscontentspf());
//			prep.setString(10, clientcontrol.getSmssourcepf());
//			prep.setString(11, clientcontrol.getMaxsms());
//			prep.setString(12, clientcontrol.getAbout());
//			prep.setString(13, clientcontrol.getNexttime());
//			prep.setInt(14, Integer.parseInt(clientid_old));
//			prep.executeUpdate();
//			conn.commit(); 
//		} catch (Exception e) {
//			logger.info("updateNewsClientcontrol fail !!!" + e.getMessage());
//			e.printStackTrace();
//			resultInt=-1;
//			if(conn!=null){
//				try {
//					conn.rollback();
//				} catch (SQLException e1) {
//					e1.printStackTrace();
//				} 
//			}
//		} finally {
//			this.closeAll();
//		}
		return resultInt;
	}
	
	
	/**
	 *  验证终端id唯一和准确
	 * @param urlB
	 * @return
	 */
	public int checkclientid_uniqueANDaccurate(String clientid,String clientid_old) {
		int iCode=0;
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsClientcontrolone n WHERE 1=1 ");
			sf.append(" and n.clientid='"+clientid+"' ");
			if(clientid_old!=null){
				 sf.append(" and n.clientid!='"+clientid_old+"'"); 
			}
			Query query = em.createQuery(sf.toString());
			if(query.getResultList().size()>0){
				//终端id已被记录
				iCode=1;
			}
			if(iCode==0){
				sf = new StringBuffer("SELECT n FROM NewsClientinfo n WHERE 1=1 ");
				sf.append(" and n.clientid='"+clientid+"' ");
				query = em.createQuery(sf.toString());
				if(query.getResultList().size()==0){
					iCode=2;
				}
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
		return iCode;
	}
	
}
